<template>
  <div class="home">
    <div
      style="
        margin: 10px 0 0 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
      "
    >
      <div>区域划分</div>
      <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>停车场管理</el-breadcrumb-item>
          <el-breadcrumb-item>停车场信息</el-breadcrumb-item>
          <el-breadcrumb-item>区域划分</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </div>

    <!-- 搜索区 重置区-->
    <div class="header">
      <el-input clearable v-model="regionNum" placeholder="区域编码"></el-input>
      <el-input clearable v-model="regionName" placeholder="区域名称"></el-input>
      <el-select clearable v-model="parkingName" placeholder="停车场">
        <el-option
          :key="v.id"
          v-for="v in parkingAll"
          :label="v.parkingName"
          :value="v.parkingName"
        ></el-option>
      </el-select>
      <el-button
        type="primary"
        class="el-icon-search"
        size="medium"
        @click="search"
        >查询</el-button
      >
      <el-button class="el-icon-refresh" size="medium" @click="reset"
        >重置</el-button
      >
    </div>

    <!-- 数据 -->
    <div style="width: auto; background: #fff">
      <div
        style="
          display: flex;
          justify-content: space-between;
          padding: 10px 0 10px 0;
        "
      >
        <p style="font-size: 15px; font-weight: bold; margin: 5px 0 0 20px">
          查询结果列表
        </p>
        <div style="margin: 0 30px 0 0">
          <el-button
            style="background: #0bb197"
            type="success"
            @click="addDialog = true"
            size="small"
            >新增</el-button
          >
        </div>
      </div>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="序号" width="100" align="center">
        </el-table-column>
        <el-table-column prop="regionNum" label="区域编号" align="center">
        </el-table-column>
        <el-table-column prop="regionName" label="区域名称" align="center">
        </el-table-column>
        <el-table-column prop="parkingName" label="停车场" align="center">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
          <template slot-scope="scope">
            <span>{{ timeTrans(scope.row.createTime, 1) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              style="color: #0bb197"
              @click="edit(scope.row), (editDialog = true)"
              type="text"
              size="small"
              >编辑</el-button
            >
            <el-button
              style="color: #0bb197"
              @click="del(scope.row.id)"
              type="text"
              size="small"
              >删除</el-button
            >
            <!-- <el-button
              style="color: #0bb197"
              @click="staFn(scope.row), (regionSet = true)"
              type="text"
              size="small"
              >区域设置</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="count"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :background="true"
        class="msg-pagination-container"
      >
      </el-pagination>
    </div>

    <!-- 新增的弹出框 -->
    <el-dialog title="新增区域" :visible.sync="addDialog">
      <el-form :model="addData">
        <div class="addBox">
          <el-form-item style="width: 45%; margin-right: 10%" label="区域编码">
            <el-input v-model="addData.regionNum" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            style="width: 45%"
            label="区域名称"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="addData.regionName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </div>

        <el-form-item label="关联停车场" style="width: 45%">
          <el-select style="width: 100%;" clearable v-model="addData.parkingNum" placeholder="停车场">
        <el-option
          :key="v.id"
          v-for="v in parkingAll"
          :label="v.parkingName"
          :value="v.parkingNum"
        ></el-option>
      </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type=""
          style="margin-right: 30px"
          class="reset"
          @click="addDialog = false"
          >取 消</el-button
        >
        <el-button type="success" class="btnColor" @click="add"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 编辑的弹出框 -->
    <el-dialog title="编辑区域" :visible.sync="editDialog">
      <el-form :model="editData">
        <div class="addBox">
          <el-form-item style="width: 45%; margin-right: 10%" label="区域编码">
            <el-input
              v-model="editData.regionNum"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item
            style="width: 45%"
            label="区域名称"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="editData.regionName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </div>

        <div class="addBox">
          <el-form-item
            label="关联停车场"
            style="width: 45%; margin-right: 10%"
          >
          <el-select style="width: 100%;" clearable v-model="editData.parkingNum" placeholder="停车场">
        <el-option
          :key="v.id"
          v-for="v in parkingAll"
          :label="v.parkingName"
          :value="v.parkingNum"
        ></el-option>
      </el-select>
          </el-form-item>
          <el-form-item label="创建时间" style="width: 45%">
            <el-input disabled v-model="editData.createTime" autocomplete="off">
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type=""
          style="margin-right: 30px"
          class="reset"
          @click="editDialog = false"
          >取 消</el-button
        >
        <el-button type="success" class="btnColor" @click="editFn"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 区域设置 -->
    <div class="regionSet">
      <el-dialog title="区域设置" :visible.sync="regionSet">
        <div class="regionSet-header">
          <!-- 搜索区 重置区-->
          <div class="header1">
            <el-select clearable v-model="user" placeholder="停车场">
        <el-option
          :key="v.id"
          v-for="v in parkingAll"
          :label="v.parkingName"
          :value="v.parkingName"
        ></el-option>
      </el-select>
            <el-select v-model="user" placeholder="选择区域">
              <el-option label="1001" value="1001"></el-option>
              <el-option label="1002" value="1002"></el-option>
              <el-option label="1003" value="1003"></el-option>
              <el-option label="1004" value="1004"></el-option>
              <el-option label="1005" value="1005"></el-option>
              <el-option label="1006" value="1006"></el-option>
            </el-select>
            <el-input v-model="id" placeholder="区域编码"></el-input>
            <el-button
              type="primary"
              class="el-icon-search"
              size="medium"
              @click="search"
              >查询</el-button
            >
            <el-button class="el-icon-refresh" size="medium" @click="reset"
              >重置</el-button
            >
          </div>
          <div class="regionSetBtn">
            <el-button type="success" size="small" style="background: #3cc1ac"
              >加入区域</el-button
            >
            <el-button
              type="success"
              size="small"
              style="background: #ff5c5c; border: 1px solid #ff5c5c"
              >删除区域</el-button
            >
          </div>
          <el-table
            border
            ref="multipleTable"
            :data="tableData5"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column label="序号" width="120" align="center">
              <template slot-scope="scope">{{ scope.row.date }}</template>
            </el-table-column>
            <el-table-column prop="name" label="车位编码" align="center">
            </el-table-column>
            <el-table-column
              prop="address"
              label="停车场"
              show-overflow-tooltip
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="address"
              label="所属区域"
              show-overflow-tooltip
              align="center"
            >
            </el-table-column>
          </el-table>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
//时间戳函数引用
import time from "../utils/time";
export default {
  mixins: [time],
  data: function () {
    return {
      page: 1,
      count: 5,
      total: 1,
      parkingName: "",
      parkingNum: "",
      regionNum: "",
      regionName: "",
      addDialog: false,
      editDialog: false,
      regionSet: false,
      formLabelWidth: "",
      id: "",
      user: "",
      currentPage4: 1,
      tableData: [],
      addData: {
        createTime: "",
        id: 0,
        parkingNum: "",
        regionName: "",
        regionNum: "",
      },
      editData: {},
      multipleSelection: [],
      tableData5: [
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
      ],
      parkingAll: [],
    };
  },
  methods: {
    //新增
    add: function () {
      // console.log('新增数据测试',this.addData)
      let nowDate = new Date().getTime();
      // console.log(nowDate)
      this.addData.createTime = nowDate;
      this.$axios({
        url: "/smart/p-region/add",
        method: "post",
        data: this.addData,
      }).then(({ data }) => {
        console.log('新增数据测试',data)
        if (data.code == '00000') {
          this.$message.success("添加成功");
          this.addDialog = false;
          this.getData();
        } else {
          this.$message.error("添加失败");
        }
      });
    },
    //编辑弹框
    edit: function (index) {
      let editData = { ...index };
      editData.createTime = this.timeTrans(editData.createTime, 1);
      this.parkingAll.forEach((v)=>{
        if(v.parkingName==editData.parkingName){
          editData.parkingNum=v.parkingNum
        }
      });
      this.editData = editData;
      console.log("编辑数据检测",this.editData);
    },
    //保存编辑
    editFn: function () {
      this.$axios({
        url: "/smart/p-region/edit",
        method: "POST",
        data: this.editData,
      }).then(({ data }) => {
        console.log("编辑数据检测", data);
        if (data.code == '00000') {
          this.$message.success("修改成功");
          this.editDialog = false;
          this.getData();
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    //删除
    del: function (index) {
      console.log(index);
      this.$axios({
        url: "/smart/p-region/del/" + index,
        method: "GET",
      }).then(({ data }) => {
        console.log("删除数据检测", data);
        if (data.code == '00000') {
          this.$message.success("删除成功");
          this.getData();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    staFn: function (n) {},
    //改变条数
    handleSizeChange(val) {
      this.count = val;
      this.getData();
      // console.log(`每页 ${val} 条`);
    },
    //翻页
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
      // console.log(`当前页: ${val}`);
    },
    //渲染
    getData: function () {
      this.$axios({
        url: "/smart/p-region/list",
        method: "GET",
        params: {
          pageNum: this.page,
          pageSize: this.count,
          regionNum: this.regionNum,
          regionName: this.regionName,
          parkingName: this.parkingName,
        },
      }).then(({ data }) => {
        console.log(data);
        if (data.code == "00000") {
          this.tableData = data.data.list;
          this.total = data.data.total;
        } else {
          this.$message.error("数据加载失败！");
        }
      });
    },
    //搜索
    search: function () {
      this.getData();
    },
    //重置
    reset: function () {
      this.regionNum = "";
      this.regionName = "";
      this.parkingName = "";
      this.getData();
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = val;
    },
  },
  mounted: function () {
    //初始化渲染
    this.getData();
    //获取所有停车场
    //获取所有停车场
    this.$axios({
        url: "/smart/parking/list",
        method: "POST",
        params: {
          pageNum: 1,
          pageSize: 10000,
        },
        data:{
          parkingNum:'',
          parkingName:'',
          parkingType:'',
        },
      }).then(({ data }) => {
        console.log("所有停车场", data);
        if (data.code == '00000') {
          this.parkingAll = data.data.list;
          console.log('挂载后测试',this.parkingAll)
        } else {
          this.$message.error("停车场加载失败！");
        }
      });
  },
};
</script>

<style lang="less" scoped>
.home {
  height: 95%;
  margin: 20px 20px 0 20px;
}
.header {
  width: 100%;
  height: 80px;
  background: #fff;
  display: flex;
  align-items: center;
  margin: 30px 0 20px 0;

  .el-input,
  .el-select {
    width: 200px;
    margin: 10px;
  }
}
.el-icon-search {
  background: #0bb197;
  margin-left: 10px;
}
.el-icon-refresh {
  background-color: white;
  color: #c0c4cc;
  border: 1px solid #c0c4cc;
}
.el-icon-refresh:hover {
  color: #0bb197;
}
.el-pagination {
  margin-top: 30px;
  text-align: right;
}

//新增弹框样式
.el-dialog {
  width: 35%;
}
.btnColor {
  background-color: #0bb197;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
.addBox {
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}
.reset {
  background-color: white;
  color: #c0c4cc;
  border: 1px solid #c0c4cc;
}
.reset:hover {
  color: #0bb197;
}
.regionSet {
  .el-dialog {
    width: 820px;
  }
}
//区域设置
.regionSet-header {
  width: 100%;
  height: 530px;

  .header1 {
    width: 100%;
    height: 80px;
    background: #fff;
    display: flex;
    align-items: center;
    // margin: 0px 0 20px 0;

    .el-input,
    .el-select {
      width: 200px;
      margin: 10px;
    }
  }

  .regionSetBtn {
    margin-left: 10px;
  }
}
</style>
<style>
.msg-pagination-container.is-background .el-pager li:not(.disabled).active {
  /*当前选中页数的样式进行修改*/
  background-color: #0bb197;
  color: #fff;
}
.regionSet .el-dialog__header {
  background: #0bb197;
  padding: 10px 20px 10px;
}
.regionSet .el-dialog__title {
  font-size: 15px;
  font-weight: bold;
  color: white;
}
.regionSet .el-dialog__headerbtn .el-dialog__close {
  color: #fff;
}
.regionSet .el-dialog__body {
  padding: 0px 20px;
}
</style>
